<template>
  <el-dialog :title="`${showText}权限`" :visible="showAddDialog" width="30%" @close="btnCancel">
    <!-- 添加弹层 -->
    <el-form ref="permForm" :model="formData" label-width="80px" :rules="rules">
      <el-form-item label="权限名称">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-button @click="btnCancel">取 消</el-button>
      <el-button type="primary" @click="btnOK">确 定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { addPermission, updatePermission } from '@/api/permisson'

export default {
  props: {
    showAddDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }

  },
  methods: {
    async btnOK() {
      if (this.formData.id) {
        // 编辑
        await updatePermission()
      }
    },
    btnCancel() {
      this.list = { // 重置表单
        name: '',
        code: '',
        description: ''
      }
      this.$emit('update:showAddDialog', false)
    }
  }
}
</script>

<style>

</style>
